package cc.mrbird.febs.buss.service.impl;

import cc.mrbird.febs.buss.entity.MainItem;
import cc.mrbird.febs.buss.entity.SumItem;
import cc.mrbird.febs.buss.service.IMainItemService;
import cc.mrbird.febs.buss.service.ISumItemService;
import cc.mrbird.febs.common.entity.FebsConstant;
import cc.mrbird.febs.common.entity.QueryRequest;
import cc.mrbird.febs.buss.entity.HallItem;
import cc.mrbird.febs.buss.mapper.HallItemMapper;
import cc.mrbird.febs.buss.service.IHallItemService;
import cc.mrbird.febs.system.entity.Dept;
import cc.mrbird.febs.system.service.IDeptService;
import org.apache.poi.ss.usermodel.Row;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.beans.factory.annotation.Autowired;
import cc.mrbird.febs.common.utils.SortUtil;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.*;

/**
 *  Service实现
 *
 * @author Lujingyuan
 * @date 2020-04-24 09:00:57
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class HallItemServiceImpl extends ServiceImpl<HallItemMapper, HallItem> implements IHallItemService {

    /*@Autowired
    private HallItemMapper hallItemMapper;*/
    @Autowired
    private IDeptService deptService;

    @Autowired
    private IMainItemService mainItemService;

    @Autowired
    private ISumItemService sumItemService;


    @Override
    public IPage<HallItem> findHallItemDetail(QueryRequest request, HashMap<String,String> map) {
        Page<HallItem> page = new Page<HallItem>(request.getPageNum(), request.getPageSize());
        SortUtil.handlePageSort(request, page, "id", FebsConstant.ORDER_ASC, false);
        return this.baseMapper.findHallItemDetailPageByMap(page,map);
    }

    @Override
    public List<HallItem> findHallItems(HallItem hallItem) {
	    LambdaQueryWrapper<HallItem> queryWrapper = new LambdaQueryWrapper<>();
		// TODO 设置查询条件
		return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    @Transactional
    public void createHallItem(HallItem hallItem) {
        this.save(hallItem);
    }

    @Override
    @Transactional
    public void updateHallItem(HallItem hallItem) {
        this.saveOrUpdate(hallItem);
    }

    @Override
    @Transactional
    public void deleteHallItem(HallItem hallItem) {
        LambdaQueryWrapper<HallItem> wrapper = new LambdaQueryWrapper<>();
	    // TODO 设置删除条件
	    this.remove(wrapper);
	}

    @Override
    @Transactional
    public void deleteHallItem(String[] ids) {
        List<String> list = Arrays.asList(ids);
        // 删除用户
        this.removeByIds(list);
    }

    @Override
    @Transactional
    public Dept findDeptByItem(Long itemId){
        Dept dept=this.baseMapper.findDeptByItem(itemId);
        return dept;
    }

    @Override
    public List<HallItem> findHallItemByMap(Map map) {
        return this.baseMapper.findHallItemByMap(map);
    }

    @Override
    public Dept getDept(String deptName) {
        LambdaQueryWrapper<Dept> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Dept::getDeptName,deptName);
        List<Dept> deptList=deptService.list(queryWrapper);
        if(deptList==null||deptList.size()==0){
            Dept dept=new Dept();
            dept.setCreateTime(new Date());
            dept.setParentId(0L);
            dept.setDeptName(deptName);
            dept.setOrderNum(1L);
            deptService.save(dept);
            return dept;
        }
        return deptList.get(0);
    }

    @Override
    public MainItem getMainItem(String name, Dept dept) {
        LambdaQueryWrapper<MainItem> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MainItem::getDepart,dept.getDeptId());
        queryWrapper.eq(MainItem::getName,name);
        List<MainItem> list=mainItemService.list(queryWrapper);
        if(list==null||list.size()==0){
            MainItem mainItem =new MainItem();
            mainItem.setDepart(dept.getDeptId());
            mainItem.setDepartName(dept.getDeptName());
            mainItem.setName(name);
            mainItemService.save(mainItem);
            return mainItem;
        }
        return list.get(0);
    }

    @Override
    public SumItem getSumItem(String name, MainItem mainItem) {
        LambdaQueryWrapper<SumItem> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SumItem::getMainItemId,mainItem.getId());
        queryWrapper.eq(SumItem::getName,name);
        List<SumItem> list=sumItemService.list(queryWrapper);
        if(list==null||list.size()==0){
            SumItem sumItem =new SumItem();
            sumItem.setMainItemId(mainItem.getId());
            sumItem.setMainItemName(mainItem.getName());
            sumItem.setName(name);
            sumItem.setDepartName(mainItem.getDepartName());
            sumItemService.save(sumItem);
            return sumItem;
        }
        return list.get(0);
    }

    @Override
    public void addItemByRow(Row row){
        if(row!=null){
            Dept dept=this.getDept(row.getCell(1).getStringCellValue());
            MainItem mainItem=this.getMainItem(row.getCell(2).getStringCellValue(),dept);
            SumItem sumItem=this.getSumItem(row.getCell(3).getStringCellValue(),mainItem);
            HallItem item=new HallItem();
            item.setSumItem(sumItem.getId());
            item.setItemType(row.getCell(4).getStringCellValue());//事项类别
            item.setName(row.getCell(5).getStringCellValue());
            item.setAddressType(row.getCell(6).getStringCellValue());//办理地址
            item.setServiceObject(row.getCell(7).getStringCellValue());//服务对象
            item.setImmediately("是".equals(row.getCell(8).getStringCellValue())?"1":"0");//即办事项
            item.setNeedCard("是".equals(row.getCell(9).getStringCellValue())?"1":"0");//凭身份证办理
            item.setAllCityHandle("是".equals(row.getCell(10).getStringCellValue())?"1":"0");//通办事项
            item.setAtMostOnce("是".equals(row.getCell(11).getStringCellValue())?"1":"0");//最多跑一次
            item.setZeroRun("是".equals(row.getCell(12).getStringCellValue())?"1":"0");//零跑动
            item.setOnlineCheck("是".equals(row.getCell(13).getStringCellValue())?"1":"0");//是否网办
            item.setBussSystem(row.getCell(14).getStringCellValue());//办理系统
            this.save(item);
        }
    }
}
